package com.recharge.srevice;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.entity.Customer;
import com.entity.Recharge;
import com.file.service.CustomerService;
import com.interfaces.ChildMeterDao;
import com.interfaces.CustomerDao;
import com.interfaces.CustomerMeterDao;
import com.interfaces.MeterDao;
import com.interfaces.RechargeDao;
import com.interfaces.SysdicDao;
import com.recharge.entity.JsonRecharge;
import com.setting.service.SysdicService;
import com.utils.JsonResult;

/**
 * @Title: ReturnRentService.java
 * @Package com.recharge.srevice
 * @author 孙梦楠
 * @date 2017年11月13日 下午1:48:10
 * @version V1.0.0
 */

@Service
public class ReturnRentService {

	@Autowired
	private RechargeDao rechargedao;

	@Autowired
	private CustomerDao customerdao;

	@Autowired
	private CustomerService customerservice;

	@Autowired
	private SysdicDao sysdicdao;

	@Autowired
	private CustomerMeterDao customermeterdao;

	@Autowired
	private MeterDao meterdao;

	@Autowired
	private SysdicService sysdicservice;

	@Autowired
	private ChildMeterDao childmeterdao;

	@Autowired
	private RechargeService rechargeservice;

	// 该org下面的退租历史记录
	@Transactional
	public JsonResult<List<JsonRecharge>> listclosing(String customertype, Integer customerorg, String languagecookie) {

		List<Recharge> recharges = rechargedao.findByCustomerorgAndDirectionAndRemark(customerorg, "退费", "退租");
		List<JsonRecharge> rechargehistories = rechargeservice.list_history(recharges, languagecookie);

		return new JsonResult<List<JsonRecharge>>(573, "退租历史", rechargehistories);
	}

	// 退租
	@Transactional
	public JsonResult<String> returnrent(Integer customerid, String opecodecookie) {
		// 充值成功的工单下发之后，再下发退租
		if (rechargeMoney0(customerid, opecodecookie)) {
			returnrentOrder(customerid, opecodecookie);
		}
		return new JsonResult<String>(801, "退租工单已下发", null);
	}

	// 充值一笔0元的工单
	@Transactional
	private boolean rechargeMoney0(Integer customerid, String opecodecookie) {

		Recharge recharge = new Recharge();
		recharge.setCustomerid(customerid);
		Customer customer = customerdao.findOne(customerid);
		Integer meterid = customermeterdao.findByCustomerid2(customerid).getPk().getMeterid();
		recharge.setMeterid(meterid);
		recharge.setCustomeraccount(customer.getCustomeraccount());
		recharge.setCustomername(customer.getCustomername());
		recharge.setCustomeraddress(customer.getCustomeraddress());
		recharge.setCustomerorg(customer.getCustomerorg());
		recharge.setCustomertype(customer.getCustomertype());
		recharge.setDirection("购电");
		recharge.setOpecode(opecodecookie);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String issuetime = sdf.format(new Date());
		recharge.setIssuetime(issuetime);
		if (rechargedao.findByCustomeridAndDirectionAndFrequencyNotOrderByFrequencyDesc(customerid, "购电", 0)
				.size() == 0) {
			recharge.setFrequency(1);

		} else {
			Integer frequency = rechargedao
					.findByCustomeridAndDirectionAndFrequencyNotOrderByFrequencyDesc(customerid, "购电", 0).get(0)
					.getFrequency();
			frequency++;
			recharge.setFrequency(frequency);
		}
		/*
		 * beforemoney 调用接口 aftermoney 接口
		 */

		recharge.setMoney(0.00);
		// Double totalmoney = rechargedao
		// .findByCustomeridAndDirectionAndFrequencyNotOrderByFrequencyDesc(customerid,
		// "购电", 0).get(0)
		// .getTotalmoney();
		// recharge.setTotalmoney(totalmoney);

		recharge.setOptstatus("等待下发");
		recharge.setOptnum(0);
		recharge.setRemark("退租");

		rechargedao.save(recharge);

		return true;
	}

	// 退租工单
	@Transactional
	private void returnrentOrder(Integer customerid, String opecodecookie) {

		Recharge recharge = new Recharge();
		recharge.setCustomerid(customerid);
		Customer customer = customerdao.findOne(customerid);
		Integer meterid = customermeterdao.findByCustomerid2(customerid).getPk().getMeterid();
		recharge.setMeterid(meterid);
		recharge.setCustomeraccount(customer.getCustomeraccount());
		recharge.setCustomername(customer.getCustomername());
		recharge.setCustomeraddress(customer.getCustomeraddress());
		recharge.setCustomerorg(customer.getCustomerorg());
		recharge.setCustomertype(customer.getCustomertype());
		recharge.setDirection("退费");
		recharge.setOpecode(opecodecookie);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String issuetime = sdf.format(new Date());
		recharge.setIssuetime(issuetime);
		if (rechargedao.findByCustomeridAndDirectionAndFrequencyNotOrderByFrequencyDesc(customerid, "退费", 0)
				.size() == 0) {
			recharge.setFrequency(1);
		} else {
			Integer frequency = rechargedao
					.findByCustomeridAndDirectionAndFrequencyNotOrderByFrequencyDesc(customerid, "退费", 0).get(0)
					.getFrequency();
			frequency++;
			recharge.setFrequency(frequency);
		}
		/*
		 * beforemoney 调用接口 aftermoney 接口
		 */

		recharge.setMoney(null);
		// Integer totalmoney =
		// rechargedao.findByCustomeridAndDirectionOrderByFrequencyDesc(customerid,"退费").get(0).getTotalmoney();
		Double totalmoney = 0.00;
		recharge.setTotalmoney(totalmoney);

		recharge.setOptstatus("等待下发");
		recharge.setOptnum(0);
		recharge.setRemark("退租");
		rechargedao.save(recharge);

	}

}
